using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System;

namespace HareGame
{
    public class UIGM : MonoBehaviour
    {
        public static UIGM Instance;
        public Button btnGM;

        public RectTransform Connect;

        public Text txtFPS;
        private Color FPSColor = Color.red;
        private float timePassed;
        private int FrameCount = 0;
        private float FPS = 0.0f;

        public Slider speed;
        public Text txtSpeed;

        public Text txtTips;

        public InputField Money;
        public InputField Diamond;
        public InputField TaskID;

        public GameObject objLogTool;
        public Text txtLog;
        public Text txtAds;
        public Text txtFreeIAP;
        public Text txtHideUI;
        public Text txtTaskID;

        private TestConfig _config;
        [SerializeField] bool isShowFPS = false;
        [SerializeField] bool isShowLog = false;
        [SerializeField] bool isShowAds = false;
        [SerializeField] bool isShowUI = true;

        public bool isFreeIAP = false;

        public bool IsUsed = false;

        private void Awake()
        {
            Instance = this;
            _config = GameEntry.Config.GetConfig<TestConfig>();
            isShowAds = _config.AdOpen;
            EnableGM(_config.ShowTestButton);
            if (_config.ShowTestButton)
            {
                if (_config.ShowFPS)
                    OnFPSClick();
                if (_config.ShowConsole)
                    OnLogClick();
            }

#if !UNITY_EDITOR
            btnGM.GetComponent<CanvasGroup>().alpha = 0;
#endif
        }

        public void EnableGM(bool enable)
        {
            btnGM.gameObject.SetActive(enable);
            objLogTool.SetActive(isShowLog);
        }

        public GameObject objGMPanel;
        private bool _needLockCameraTouch;

        public void ShowGMPanel()
        {
            IsUsed = true;
            txtTips.text = "";
            Money.text = "";
            Diamond.text = "";
            txtSpeed.text = $"游戏速度:<color=green>{speed.value}</color>";
            txtTaskID.text = $"当前任务ID:<color=green>{GameEntry.Task.GetFirstTaskData().ID}</color>";
            objGMPanel.SetActive(true);
            txtFreeIAP.text = "免内购" + (isFreeIAP ? "(<color=green>ON</color>)" : "(<color=red>OFF</color>)");
            txtAds.text = "广告" + (isShowAds ? "(<color=green>ON</color>)" : "(<color=red>OFF</color>)");
            _needLockCameraTouch = GameEntry.Camera.IsEnableCameraTouch;
            if (_needLockCameraTouch)
                GameEntry.Camera.EnableCameraTouch(false);
            LayoutRebuilder.ForceRebuildLayoutImmediate(Connect);
        }

        public void HideGMPanel()
        {
            objGMPanel.SetActive(false);
            if (_needLockCameraTouch)
                GameEntry.Camera.EnableCameraTouch(true);
        }

        public void OnAdsClick()
        {
            isShowAds = !isShowAds;
            txtAds.text = "广告" + (isShowAds ? "(<color=green>ON</color>)" : "(<color=red>OFF</color>)");
            _config.AdOpen = isShowAds;
        }

        public void OnFPSClick()
        {
            isShowFPS = !isShowFPS;
            txtFPS.text = "FPS" + (isShowFPS ? "(<color=green>ON</color>)" : "(<color=red>OFF</color>)");
        }

        public void OnFreeIAPClick()
        {
            isFreeIAP = !isFreeIAP;
            txtFreeIAP.text = "免内购" + (isFreeIAP ? "(<color=green>ON</color>)" : "(<color=red>OFF</color>)");
        }

        public void OnLogClick()
        {
            isShowLog = !isShowLog;
            txtLog.text = "Log" + (isShowLog ? "(<color=green>ON</color>)" : "(<color=red>OFF</color>)");
            objLogTool.SetActive(isShowLog);
        }

        public void OnShowUIClick()
        {
            isShowUI = !isShowUI;
            txtHideUI.text = isShowUI ? "隐藏UI" : "显示UI";
            UIRoot.Instance.CanvasGroup.enabled = !isShowUI;
        }

        public void SetBigSize()
        {
            GameEntry.Camera.BigSize();
        }

        public void ResetSize()
        {
            GameEntry.Camera.ResetSize();
        }

        public void SetTaskID()
        {
            foreach (var v in GameEntry.Data.GetAllTaskData())
            {
                v.IsCompleted = false;
            }
            int taskID = Convert.ToInt32(TaskID.text);
            GameEntry.Task._curStateID = GameEntry.Data.GetTaskData("task_" + taskID).StateID;
            GameEntry.GameData.SetInt(UserData.TaskStateID, GameEntry.Data.GetTaskData("task_" + taskID).StateID);
            for (int i = 1; i < taskID; i++)
            {
                GameEntry.Data.GetTaskData("task_" + i).IsCompleted = true;
            }
            GameEntry.Event.Send(EventID.RefreshDataUI);
            GameEntry.Event.Send(EventID.RefreshTaskUI);
            txtTips.text = "任务ID设置成功:" + TaskID.text;
            txtTaskID.text = $"当前任务ID:<color=green>{GameEntry.Task.GetFirstTaskData().ID}</color>";
        }

        public void ClearData()
        {
            //清除实名认证数据
            GameEntry.SDK.ClearUserData();
            Application.Quit();
        }

        public void SetMoney()
        {
            GameEntry.Data.Money = double.Parse(Money.text);
            GameEntry.Event.Send(EventID.RefreshDataUI);
            txtTips.text = "钞票设置成功:" + Money.text;
        }

        public void SetDiamond()
        {
            GameEntry.Data.UseDiamond(GameEntry.Data.Diamond);
            GameEntry.Data.AddDiamond(Convert.ToInt32(Diamond.text));
            txtTips.text = "钻石设置成功:" + Diamond.text;
        }

        public void OnSpeedChange()
        {
            Time.timeScale = speed.value;
            txtSpeed.text = $"游戏速度:<color=green>{speed.value}</color>";
        }

        void OnGUI()
        {
            if (isShowFPS)
            {
                GUIStyle style = new GUIStyle();
                style.normal.background = null;
                if (FPS < 30)
                {
                    FPSColor = Color.red;
                }
                else if (FPS >= 30 && FPS < 45)
                {
                    FPSColor = Color.yellow;
                }
                else
                {
                    FPSColor = Color.green;
                }

                style.normal.textColor = FPSColor;
                style.fontSize = 35;
                GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height - 100, 200, 200), "FPS: " + FPS.ToString("F0"), style);
            }
        }

        void Update()
        {
            FrameCount = FrameCount + 1;
            timePassed = timePassed + Time.deltaTime;

            if (timePassed > 1)
            {
                FPS = FrameCount * Time.timeScale / timePassed;

                timePassed = 0.0f;
                FrameCount = 0;
            }
        }
    }
}